package net.hn.hnms.biz.emgcy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.EmgcyLocatePo;
import net.hn.hnms.biz.emgcy.domain.dto.EmgcyLocateDto;
import net.hn.hnms.biz.emgcy.domain.vo.EmgcyLocateVo;
import net.hn.hnms.biz.emgcy.service.EmgcyLocateService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.file.api.RemoteFileService;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Api(value = "现场处置模块", tags = {"现场处置"})
@RestController
@RequestMapping("emgcyLocate")
public class EmgcyLocateController {
    @Autowired
    private EmgcyLocateService emgcyLocateService;
    @Autowired
    private RemoteDictService remoteDictService;
    @Autowired
    private RemoteFileService remoteFileService;

    @ApiOperation("现场处置方案列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<EmgcyLocateVo>> pageList(PageQuery pageQuery, EmgcyLocateDto emgcyLocateDto) {
        ResponsePage<EmgcyLocateVo> list=emgcyLocateService.pageList(pageQuery,emgcyLocateDto);
        return R.ok(list);
    }

    @ApiOperation("现场处置方案添加")
    @PostMapping("/add")
    public R add(@RequestBody EmgcyLocatePo emgcyLocatePo){
        QueryWrapper<EmgcyLocatePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("event_type", emgcyLocatePo.getEventType());
        EmgcyLocatePo one = emgcyLocateService.getOne(queryWrapper);
        if (one != null) {
            String event_type = remoteDictService.selectDictLabel("event_type", one.getEventType());
            throw new HuaNengException(500,"已有"+event_type+"对应的现场处置方案");
        }
        Boolean flag = emgcyLocateService.add(emgcyLocatePo);
        if(flag){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("现场处置方案修改")
    @PutMapping("/update")
    public R update(@RequestBody EmgcyLocatePo emgcyLocatePo){
        Boolean flag = emgcyLocateService.update(emgcyLocatePo);
        if(flag){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    @ApiOperation("现场处置方案删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        EmgcyLocatePo emgcyLocatePo = emgcyLocateService.getById(id);
        remoteFileService.remove(new Long[]{emgcyLocatePo.getInformationId().longValue()});
        Boolean flag = emgcyLocateService.delete(id);
        if(flag){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
